#include "segment_header_widget.h"
#include <QHBoxLayout>
SegmentHeaderWidget::SegmentHeaderWidget(QWidget *parent)
    : QWidget{parent}
    ,tableWidget(new QTableWidget(this))
{
    initUI();
}

void SegmentHeaderWidget::updateData(std::vector<ELF_PHDR> vecElfPhdr)
{
    tableWidget->clear();
    tableWidget->setColumnCount(9);
    tableWidget->setRowCount(vecElfPhdr.size());
    for(int i = 0 ; i < vecElfPhdr.size() ;i ++){
        QTableWidgetItem *itemId = new QTableWidgetItem(QString::number(i));
        QTableWidgetItem *itemType = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentType.c_str()));
        QTableWidgetItem *itemOffset = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentOffset.c_str()));
        QTableWidgetItem *itemVaddr = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentVaddr.c_str()));
        QTableWidgetItem *itemPaddr = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentPaddr.c_str()));
        QTableWidgetItem *itemFilesz = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentFilesz.c_str()));
        QTableWidgetItem *itemMemsz = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentMemsz.c_str()));
        QTableWidgetItem *itemFlags = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentFlags.c_str()));
        QTableWidgetItem *itemAlign = new QTableWidgetItem(QString(vecElfPhdr[i].strSegmentAlign.c_str()));

        tableWidget->setItem(i, 0, itemId);
        tableWidget->setItem(i, 1, itemType);
        tableWidget->setItem(i, 2, itemOffset);
        tableWidget->setItem(i, 3, itemVaddr);
        tableWidget->setItem(i, 4, itemPaddr);
        tableWidget->setItem(i, 5, itemFilesz);
        tableWidget->setItem(i, 6, itemMemsz);
        tableWidget->setItem(i, 7, itemFlags);
        tableWidget->setItem(i, 8, itemAlign);
    }
}

void SegmentHeaderWidget::initUI()
{
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addWidget(tableWidget);
    setLayout(mainLayout);
}
